﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WikiTrailCreator.WikiGeneration
{
    public partial class ProgressBarForm : Form
    {
        private static Thread m_thread;
        private static ProgressBarForm m_form = null;
        private static ManualResetEvent m_eventFormCreated = new ManualResetEvent(false);

        private delegate void SetPropertyToIntDelegate(int val);
        private delegate void SetPropertyToStringDelegate(string val);

        public ProgressBarForm()
        {
            InitializeComponent();
        }

        public static void ShowDialogInSeparateThread(Form frm)
        {
            object param = frm;
            m_thread = new Thread(new ParameterizedThreadStart(StartLoadingBarThread));
            m_thread.Start(param);
            m_eventFormCreated.WaitOne();
        }

        private static void StartLoadingBarThread(object param)
        {
            Form frm = param as Form;

            if (frm != null)
            {
                m_form = new ProgressBarForm();
                m_form.HandleCreated += new EventHandler(m_form_HandleCreated);
                m_form.ShowDialog();
            }
        }

        private static void m_form_HandleCreated(object sender, EventArgs args)
        {
            m_eventFormCreated.Set();
        }

        public int MaxProgress
        {
            get
            {
                return progressBar1.Maximum;
            }
            set
            {

                progressBar1.Maximum = value;
            }
        }

        private void SetMaxProgressInternal(int val)
        {
            this.MaxProgress = val;
        }

        public static void SetMaxProgress(int val)
        {
            m_form.Invoke(new SetPropertyToIntDelegate(m_form.SetMaxProgressInternal), new object[] { val });
        }

        public int CurrentProgress
        {
            get
            {
                return progressBar1.Value;
            }
            set
            {
                progressBar1.Value = value;
                this.Refresh();
            }
        }

        private void IncCurrentProgressInternal()
        {
            this.CurrentProgress++;
            this.BringToFront();
        }

        public static void IncCurrentProgress()
        {
            m_form.Invoke(new MethodInvoker(m_form.IncCurrentProgressInternal));
        }

        public string DisplayText
        {
            get
            {
                return label1.Text;
            }
            set
            {
                label1.Text = value;
                this.Refresh();
            }
        }

        private void SetDisplayTextInternal(string val)
        {
            this.DisplayText = val;
            this.BringToFront();
        }

        public static void SetDisplayText(string val)
        {
            m_form.Invoke(new SetPropertyToStringDelegate(m_form.SetDisplayTextInternal), new object[] { val });
        }

        public static void CloseForm()
        {
            if (m_form != null)
            {
                m_form.Invoke(new MethodInvoker(m_form.Close));
                m_form = null;
                m_thread = null;
                m_eventFormCreated = new ManualResetEvent(false);
            }
        }
    }
}
